package com.schrodinger.zwebservice.axis2.client.wscaller.impl;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import com.schrodinger.zwebservice.axis2.client.util.WSClientUtils;
import com.schrodinger.zwebservice.axis2.client.wscaller.IWSInvoker;
import com.schrodinger.zwebservice.axis2.client.wsconfig.WSConfigManager;
import com.schrodinger.zwebservice.axis2.client.wsconfig.WSConfigInfo;


/**
 * 用于调用OA接口为移动OA做数据交换
 * @author chenchen
 *
 */
public class WSInvokerImpl implements IWSInvoker {
	private EndpointReference targetEPR = null;
	private RPCServiceClient serviceClient = null;
	private Long  WS_REQUEST_TIMEOUT = null;
	
	/**
	 * 初始化WS调用环境
	 * @param serveiceName
	 * @throws AxisFault
	 */
	public WSInvokerImpl(String serviceMark, String serveiceName) throws AxisFault{
		WSConfigInfo serverInfo = WSConfigManager.getItem(serviceMark);		
		targetEPR = new EndpointReference(serverInfo.END_POINT_URL + serveiceName);
		serviceClient = new RPCServiceClient();
		WS_REQUEST_TIMEOUT = serverInfo.WS_REQUEST_TIMEOUT;
		Options options = serviceClient.getOptions();
		options.setTo(targetEPR);
		options.setTimeOutInMilliSeconds(this.WS_REQUEST_TIMEOUT); 
				
	}

	public Object invokerMethodHasReturn(String classNameWithPackage,
			String methodName, Object[] args, Class[] returnValueClass) throws AxisFault {
		String namespace = WSClientUtils.getQNameSpace(classNameWithPackage);
		QName opAddEntry = new QName(namespace, methodName);
		Options options = new Options(); 
		options.setTimeOutInMilliSeconds(this.WS_REQUEST_TIMEOUT); 
		options.setTo(targetEPR);
		serviceClient.setOptions(options);
		return serviceClient.invokeBlocking(opAddEntry, args, returnValueClass)[0];
	}

	public Object invokeMethodHasReturn(String targetNameSpace,
			String methodName, Object[] args, Class[] returnValueClass) throws AxisFault {
		QName opAddEntry = new QName(targetNameSpace, methodName);
		Options options = new Options(); 
		options.setTimeOutInMilliSeconds(this.WS_REQUEST_TIMEOUT); 
		options.setTo(targetEPR);
		serviceClient.setOptions(options);
		return serviceClient.invokeBlocking(opAddEntry, args, returnValueClass)[0];
	}
	
	public void invokerMethodNoReturn(String classNameWithPackage,
			String methodName, Object[] args) throws AxisFault {
		Options options = new Options(); 
		options.setTimeOutInMilliSeconds(this.WS_REQUEST_TIMEOUT); 
		options.setTo(targetEPR);
		options.setAction("urn:" + methodName); 
		
		String namespace = WSClientUtils.getQNameSpace(classNameWithPackage);
		QName opAddEntry = new QName(namespace, methodName);
		
		serviceClient.invokeRobust(opAddEntry, args);
	}
	
	public void invokeMethodNoReturn(String targetNameSpace,
			String methodName, Object[] args) throws AxisFault {
		Options options = new Options(); 
		options.setTimeOutInMilliSeconds(this.WS_REQUEST_TIMEOUT); 
		options.setTo(targetEPR);
		options.setAction("urn:" + methodName); 
		
		QName opAddEntry = new QName(targetNameSpace, methodName);
		
		serviceClient.invokeRobust(opAddEntry, args);
	}
}
